#include "stdafx.h"
#include "XMLActions.hxx"

#define XML_STATIC
#include <stdlib.h>
#include <string.h>
#include <expat/expat.h>
#include <stdio.h>
#include <math.h>
#include "AppSetting.hxx"
#include <string>

using namespace std;

HostInfo __gHostInfo[3];

static void XMLCALL

startElement(void *userData, const char *name, const char **atts)
{
    if( strcmp( name , "Host") == 0 )
    {
		int id = 0;
		int port = 0;
		
		std::string ipAddress;
		std::string userName;
		std::string passwd;
		std::string file;

        for( int i = 0; atts[ i ]; i += 2 )
        {
            if( strcmp( atts[ i ], "id") == 0 )
			{
				sscanf_s( atts[ i + 1 ], "%d", &id );
			}
			else if( strcmp( atts[ i ], "ipaddr" ) == 0 )
			{
				ipAddress = atts[ i + 1 ];
			}
			else if( strcmp( atts[ i ], "port" ) == 0 )
			{
				sscanf_s( atts[ i + 1 ], "%d", &port );
			}
			else if( strcmp( atts[ i ], "user" ) == 0 )
			{
				userName = atts[ i + 1 ];
			}
			else if( strcmp( atts[ i ], "passwd" ) == 0 )
			{
				passwd = atts[ i + 1 ];
			}
			else if( strcmp( atts[ i ], "file" ) == 0 )
			{
				file = atts[ i + 1 ];
			}
        }
		
		int index = id - 1;

		__gHostInfo[ index ].Port = port;

		strcpy_s( __gHostInfo[ index ].IpAddress, 19, ipAddress.c_str() );
		strcpy_s( __gHostInfo[ index ].UserName, 19, userName.c_str() );
		strcpy_s( __gHostInfo[ index ].Password, 19, passwd.c_str() );
		strcpy_s( __gHostInfo[ index ].File, 19, file.c_str() );

    }
}

static void XMLCALL
endElement(void *userData, const char *name)
{
}

/////////////////////////////Tian/////////////////////////////////////////
void loadHostInfo2()
{
	if (appSetting.m_SourceDataType == CAP_DATA)
	{
		list<HostSettings>::iterator iter;
		for (iter=appSetting.m_HostSettings.begin();iter != appSetting.m_HostSettings.end(); iter++)
		{
			const HostSettings& hostSetting=*iter;
			int index = hostSetting.id - 1;
			__gHostInfo[ index ].Port = hostSetting.port;

			strcpy_s( __gHostInfo[ index ].IpAddress, 19, hostSetting.ipaddr.c_str() );
			strcpy_s( __gHostInfo[ index ].UserName, 19,  hostSetting.user.c_str() );
			strcpy_s( __gHostInfo[ index ].Password, 19, hostSetting.passwd.c_str() );
			strcpy_s( __gHostInfo[ index ].File, 19, hostSetting.paramfile.c_str() );
		}
	}else if (appSetting.m_SourceDataType == MP4_DATA)
	{
		list<Mp4Settings>::iterator iter;
		
		for (iter=appSetting.m_Mp4Settings.begin();iter != appSetting.m_Mp4Settings.end();iter++)
		{
			const Mp4Settings& mp4Setting = *iter;
			int index = mp4Setting.ChannelId - 1;
			strcpy_s( __gHostInfo[ index ].File, 19, mp4Setting.paramfile.c_str() );
			strcpy_s( __gHostInfo[ index ].SourceFile, 19, mp4Setting.sourcefile.c_str() );
		}
	}else if (appSetting.m_SourceDataType == YUV_DATA)
	{
		list<YUVSettings>::iterator iter;
		for (iter=appSetting.m_YUVSettings.begin();iter != appSetting.m_YUVSettings.end();iter++)
		{
			const YUVSettings& yuvSetting = *iter;
			int index = yuvSetting.ChannelId - 1;
			strcpy_s( __gHostInfo[ index ].File, 19, yuvSetting.paramfile.c_str() );
			strcpy_s( __gHostInfo[ index ].SourceFile, 19, yuvSetting.sourcefile.c_str() );
		}
	}else
	{
		printf("load project param failed!\n");
	}
}



void loadAppSetting(const char* pFilename)
{
	appSetting.load(pFilename);
	loadHostInfo2();
	loadPoints2();

}

void saveAppSetting()
{

}

//////////////////////////////////////////////////////////////////////////

void loadHostInfo()
{
    // Parse Sequences
    char buf[ 4096 ];
    XML_Parser parser = XML_ParserCreate(NULL);
    int done;

    XML_SetUserData(parser, 0 );
    XML_SetElementHandler(parser, startElement, endElement);

    //Open Paramter file
    FILE* fprj = 0;
	fopen_s( &fprj, "Hosts.xml" , "r" );
    if( !fprj )
    {
        printf("Read Camera Setting file error!!!\n");
        return;
    }

    do
    {
        int len = (int)fread(buf, 1, sizeof(buf), fprj);
        done = len < sizeof(buf);
        printf("%d\n",len);
        if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR)
        {
            printf("%s at line %d\n",XML_ErrorString(XML_GetErrorCode(parser)),XML_GetCurrentLineNumber(parser));
            return;
        }
    } while (!done);

    XML_ParserFree(parser);
    fclose( fprj );
}

void writeBoxInfo()
{
	FILE* file = fopen("BoxInfo.conf","w");

	if( file )
	{
		for( int i = 0; i < 3; i ++ )
		{
			BoxRect rect = __gHostInfo[ i ].FrontBox;
			fprintf( file,"%d %d %f %f %f %f\r\n", i + 1, 0, rect.X, rect.Y, rect.Width, rect.Height );
			rect = __gHostInfo[ i ].BackBox;
			fprintf( file,"%d %d %f %f %f %f\r\n", i + 1, 1, rect.X, rect.Y, rect.Width, rect.Height );			
		}

		fclose( file );
	}
}

void loadBoxInfo()
{
	FILE* file = fopen("BoxInfo.conf","r");

	if( file )
	{
		int index = 0;
		int flag = 0;
		float tx = 0;
		float ty = 0;
		float tw = 0;
		float th = 0;

		BoxRect rect;

		for( int i = 0; i < 6; i ++ )
		{
			fscanf( file,"%d %d %f %f %f %f\r\n", &index, &flag, &tx, &ty, &tw, &th );

			rect.X = tx;
			rect.Y = ty;
			rect.Width = tw;
			rect.Height = th;

			if( flag == 0 )
				__gHostInfo[ index - 1 ].FrontBox = rect;
			else
				__gHostInfo[ index - 1 ].BackBox = rect;
		}

		fclose( file );
	}
}

void writePoints()
{
	int j=0;
   FILE* file = fopen("Points.conf","w");

	if( file )
	{
		for( int i = 0; i < 3; i ++ )
		{		

			fprintf( file,"%d %d ", i + 1, 0 );
			for(j=0;j<8;j++)
				fprintf( file," %f ",__gHostInfo[ i ].frontPoints[j]);
			fprintf( file," \r\n");	

			fprintf( file,"%d %d ", i + 1, 1 );
			for(j=0;j<8;j++)
				fprintf( file," %f ",__gHostInfo[ i ].backPoints[j]);
			fprintf( file," \r\n");			
		}

		fclose( file );
	}
}

void loadPoints2()
{
	list<BoxCalibrationSettings>::iterator iter;
	for (iter = appSetting.m_BoxCalibSetting.begin();iter != appSetting.m_BoxCalibSetting.end();iter++)
	{
		BoxCalibrationSettings& boxSetting = *iter;
		int index = boxSetting.ChannelId - 1;
		//__gHostInfo[index].frontPoints = (float*) malloc (sizeof(float)*16);
		__gHostInfo[index].frontPoints = boxSetting.frontPoint;
		__gHostInfo[index].backPoints = boxSetting.backPoint;
		
	}
}


void loadPoints()
{
	int i=0;

	FILE* file = fopen("Points.conf","r");

	if( file )
	{
		int index = 0;
		int flag = 0;

		float point[8];

		/*for(int i=0;i<3;i++)
		{
		    __gHostInfo[ i].frontPoints=(float *)malloc(sizeof(float)*10);
			__gHostInfo[ i].backPoints=(float *)malloc(sizeof(float)*10);
		}*/

		for( int i = 0; i < 6; i ++ )
		{

			fscanf( file,"%d %d", &index, &flag );
			for(int k=0;k<8;k++)
			fscanf( file," %f",&point[k]); 
			fscanf( file," \r\n"); 
					

			if( flag == 0)
			{
				__gHostInfo[ index - 1 ].frontPoints=(float *)malloc(sizeof(float)*10);
			    for(int j=0;j<8;j++)	
					__gHostInfo[ index - 1 ].frontPoints[j]=point[j];
				
			}				
			else 
			{
				__gHostInfo[ index - 1 ].backPoints=(float *)malloc(sizeof(float)*10);
			    for(int j=0;j<8;j++)				
					__gHostInfo[ index - 1 ].backPoints[j]=point[j];
				
			}	
		}

		fclose( file );
	}
}

